package com.xsp.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.xsp.entity.CommonResult;
import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xsp
 * @date 2024/2/5
 * @desc 处理和响应检查用户重名请求的控制器
 * 使用了自由度更高
 */
public class User1Controller implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        // 获取提交的用户名
        String username = request.getParameter("username");
        // 假设admin已被使用-模拟
        if ("admin".equals(username)){
            response.getWriter().print(JSON.toJSONString(CommonResult.error("用户名已被使用")));
        }else{
            response.getWriter().print(JSON.toJSONString(CommonResult.success("用户名可以使用")));
        }
    }
}
